﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using eCRAM.Model;

namespace eCRAM.WebSite.Models
{
    public class UserModel
    {
        [Required]
        public Guid Id { get; set; }

        [Required]
        [DisplayName("Login")]
        public  string Login { get; set; }

        [Required]
        [DisplayName("Prénom")]
        public  string FirstName { get; set; }

        [Required]
        [DisplayName("Nom")]
        public  string LastName { get; set; }

        [DisplayName("Société")]
        public  string Company { get; set; }
 
        [Required]
        [DisplayName("Actif")]
        public  bool IsActive { get; set; }

        [Required]
        [DisplayName("Date de création")]
        public  DateTime CreationDate { get; set; }

        [Required]
        [DisplayName("Rôle")]
        public  int Role { get; set; }

        [Required]
        [DisplayName("E-Mail")]
        public string Email { get; set; }

        /// <summary>
        /// Liste des activités associées
        /// </summary>
        public IEnumerable<UserActivityModel> UserActivities { get; set; }

        /// <summary>
        /// Création d'un modèle à partir de l'entité BD
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        internal static UserModel FromUser(User user)
        {
            return new UserModel
            {
                Id = user.Id,
                FirstName = user.FirstName,
                LastName = user.LastName,
                Login = user.Login,
                Company = user.Company,
                CreationDate = user.CreationDate,
                Email = user.Email,
                IsActive = user.IsActive,
                Role = user.Role,
                UserActivities = user.User_Activities.Select(UserActivityModel.FromUserActivity) 
            };
        }

        internal User ToUser()
        {
            return new User
            {
                Id = Id,
                FirstName = FirstName,
                LastName = LastName,
                Login = Login,
                Company = Company,
                CreationDate = CreationDate,
                Email = Email,
                IsActive = IsActive,
                Role = Role
            };
        }
    }
}